﻿
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataService.Office.Word
{
    public static class WordPageNum
    {
        /// <summary>
        /// 设置连续页码
        /// </summary>
        public static void SetPageNum(MainDocumentPart mainDocumentPart, Body body)
        {
            // 遍历文档的所有节
            //foreach (SectionProperties sectionProperties in body.Elements<SectionProperties>())
            //{
            //}
            var footerParts = mainDocumentPart.GetPartsOfType<FooterPart>();
            if (footerParts == null)
            {
                return;
            }
            // 找到页脚
            FooterPart footerPart = footerParts.FirstOrDefault();
            if (footerPart != null)
            {
                // 创建新的Run，包含页码
                var footer = footerPart.Footer;
                #region 第一种，好像不起作用
                //FieldCode fieldCode = new FieldCode(" PAGE \\* MERGEFORMAT ");
                //var paragraph = new Paragraph(new Run(fieldCode));
                #endregion

                #region 第二种
                // 添加页码
                Paragraph paragraph = new Paragraph();
                paragraph.Append(new Run(new Text("第") { Space = DocumentFormat.OpenXml.SpaceProcessingModeValues.Preserve }));
                paragraph.Append(new Run(new FieldChar() { FieldCharType = FieldCharValues.Begin }));
                paragraph.Append(new Run(new FieldCode(" PAGE \\* MERGEFORMAT ")));
                paragraph.Append(new Run(new FieldChar() { FieldCharType = FieldCharValues.Separate }));
                paragraph.Append(new Run(new FieldChar() { FieldCharType = FieldCharValues.End }));
                paragraph.Append(new Run(new Text("页") { Space = DocumentFormat.OpenXml.SpaceProcessingModeValues.Preserve }));
                var ppr = new ParagraphProperties(new Justification() { Val = JustificationValues.Center });
                paragraph.Append(ppr);
                //Paragraph paragraph = new Paragraph();
                //Run run = new Run(new Text("第"));
                //run.Append(new FieldChar() { FieldCharType = FieldCharValues.Begin });
                //run.Append(new FieldCode(" PAGE \\* MERGEFORMAT "));
                //run.Append(new Text("页"));
                //paragraph.Append(run);
                //paragraph.Append(new Run(new FieldChar() { FieldCharType = FieldCharValues.Separate }));
                //paragraph.Append(new Run(new FieldChar() { FieldCharType = FieldCharValues.End }));
                #endregion

                // 如果页脚是空的或者没有Paragraph，就添加新的Paragraph
                if (footer.ChildElements.Count == 0 || !(footer.ChildElements.First() is Paragraph))
                {
                    footer.AppendChild(paragraph);
                }
                else
                {
                    footer.PrependChild<Paragraph>(paragraph);
                }

                // footerPart.Footer = footer;
            }

        }

        /// <summary>
        /// 是否设置连续页码
        /// </summary>
        public static bool IsPageNum(MainDocumentPart mainDocumentPart, Body body)
        {

            var footers = mainDocumentPart.FooterParts;
            if (footers != null)
            {
                foreach (var footer in footers)
                {
                    if (footer.Footer != null)
                    {
                        var paras = footer.Footer.Descendants<DocumentFormat.OpenXml.Wordprocessing.Paragraph>();
                        foreach (var para in paras)
                        {
                            var fieldChars = para.Descendants<FieldChar>();
                            foreach (FieldChar fieldChar in fieldChars)
                            {
                                if (fieldChar.FieldCharType == FieldCharValues.Begin)
                                {
                                    FieldCode fieldCode = para.Descendants<FieldCode>().FirstOrDefault();
                                    if (fieldCode != null && fieldCode.Text.Contains("\\*"))
                                    {
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //来到这里，说明没有找到，继续查找， 遍历所有段落，查找连续页码设置

            foreach (Paragraph para in body.Elements<Paragraph>())
            {
                var fieldChars = para.Descendants<FieldChar>();
                foreach (FieldChar fieldChar in fieldChars)
                {
                    if (fieldChar.FieldCharType == FieldCharValues.Begin)
                    {
                        FieldCode fieldCode = para.Descendants<FieldCode>().FirstOrDefault();
                        if (fieldCode != null && fieldCode.Text.Contains("\\*"))
                        {
                            return true;
                        }
                    }
                }
            }


            #region 这种找不到代码域
            //foreach (Paragraph para in body.Elements<Paragraph>())
            //{
            //    foreach (Run run in para.Elements<Run>())
            //    {
            //        foreach (FieldChar fieldChar in run.Elements<FieldChar>())
            //        {
            //            if (fieldChar.FieldCharType == FieldCharValues.Begin)
            //            {
            //                FieldCode fieldCode = run.Elements<FieldCode>().FirstOrDefault();
            //                if (fieldCode != null && fieldCode.Text.Contains("\\*"))
            //                {
            //                    return true;
            //                }
            //            }
            //        }
            //    }
            //}
            #endregion

            return false;
        }
    }
}
